home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-03-16 | 5.2 KB | 212 lines | [TEXT/MMCC] |
- //---------------------------------------------------------------------
- //---------------------------------------------------------------------
- //
- // Horrible Rickety Shell, by Dave Johnson
- //
- // © Copyright 1985 - 1995 Anyone Who Wants It,
- // All Rights Energetically Hurled as far away from me as possible.
- // Use at your own (considerable) risk.
-
-
- #include "DialogUtils.h"
-
- /* Most of this code is from C.K. Haun's DialogBits snippet, but of course has
- been tweaked for my own purposes */
-
- // UPPs
- UserItemUPP gBtnOutlineUPP;
- ModalFilterUPP gStdKeyFilterUPP, gNumFilterUPP;
-
- void InitDialogUPPs(void)
- {
- // Build UPPs in heap
- gBtnOutlineUPP = NewUserItemProc(BtnItem);
- gStdKeyFilterUPP = NewModalFilterProc(StdKeyFilter);
- gNumFilterUPP = NewModalFilterProc(NumFilter);
- }
-
- /* This filter proc only allows numeric input, and also does the standard key
- filtering */
- pascal Boolean NumFilter(DialogPtr dptr, EventRecord *event, short *item)
- {
- char theKey;
- WindowPtr temp;
- Boolean returnVal = false;
-
- GetPort(&temp);
- SetPort(dptr);
-
- // Change the cursor to an I Beam if it's over the active editText item
- IBeamIt(dptr);
-
- // Standard key filtering
- returnVal = StdKeyFilter(dptr, event, item);
-
- // if that didn't handle it...
- if(returnVal == false)
- {
- // We're only allowing numeric characters
- if ((event->what == keyDown) || (event->what == autoKey))
- {
- theKey = event->message & charCodeMask;
- if(theKey > kLastCntrlKey && theKey < kDeleteKey) // Printable Ascii?
- {
- if (theKey < '0' || theKey > '9') // not a number?
- {
- SysBeep(1); // complain a little
- returnVal = true;
- }
- }
- else
- returnVal = false;
- }
- }
- SetPort(temp);
- return(returnVal);
- }
-
- /* Standard key filtering: return or enter hit the OK button, escape hits cancel.
- Also Hilite the button for visual feedback. */
- pascal Boolean StdKeyFilter(DialogPtr dptr, EventRecord *event, short *item)
- {
- long tilticks;
- char theKey;
- Boolean returnVal = false;
-
- if ((event->what == keyDown) || (event->what == autoKey))
- {
- theKey = event->message & charCodeMask;
- switch (theKey)
- {
- // return or enter hits the OK button
- case kReturnKey:
- case kEnterKey:
- *item = iOKButton;
- // now we need to invert the button
- HiliteControl(SnatchHandle(dptr, iOKButton), inButton);
- Delay(8, &tilticks); // wait about 8 ticks so they can see it
- HiliteControl(SnatchHandle(dptr, iOKButton), 0);
- returnVal = true;
- break;
-
- // Escape hits the cancel button
- case kEscKey:
- *item = iCancelButton;
- HiliteControl(SnatchHandle(dptr, iCancelButton), inButton);
- Delay(8, &tilticks); // wait about 8 ticks so they can see it
- HiliteControl(SnatchHandle(dptr, iCancelButton), 0);
- returnVal = true;
- break;
-
- default:
- break;
- }
- }
- return returnVal;
- }
-
- void IBeamIt(WindowPtr dwind)
- {
- Point thePt;
- short kind;
- Handle itmhndl;
- Rect rect;
- short itemNum;
-
- // first get the current edit line out of the dialog record
- itemNum = ((DialogPeek)dwind)->editField + 1; // always stored 1 less
- GetDItem(dwind, itemNum, &kind, &itmhndl, &rect);
- GetMouse(&thePt);
- if (PtInRect(thePt, &rect))
- {
- SetCursor(*(GetCursor(iBeamCursor)));
- }
- else
- {
- SetCursor(&qd.arrow);
- }
- }
-
- void ShortToDlog(short val, DialogPtr dptr, short item)
- {
- short kind;
- Handle itmhndl;
- Rect rect;
- Str255 tempstr;
-
- NumToString((long)val, tempstr);
- GetDItem(dptr, item, &kind, &itmhndl, &rect);
- SetIText(itmhndl, tempstr);
- }
-
- short DlogToShort(DialogPtr dptr, short itmnum)
- {
- Handle itmhndl;
- Rect rect;
- Str255 tempstr;
- short kind;
- long temp;
-
- GetDItem(dptr, itmnum, &kind, &itmhndl, &rect);
- GetIText(itmhndl, tempstr);
- StringToNum(tempstr, &temp);
- return (short)temp;
- }
-
- pascal void BtnItem(DialogPtr dptr, short item)
- {
- short type;
- Rect rect;
- Handle hndl;
- PenState old;
-
- GetPenState(&old);
- PenSize(3, 3);
- GetDItem(dptr, iOKButton, &type, &hndl, &rect);
- InsetRect(&rect, -4, -4);
- FrameRoundRect(&rect, 16, 16);
- SetPenState(&old);
- }
-
- /* Gets the ControlHandle for the item you want in the dialog box thebox.
- Handy for setting checkboxes and radio buttons */
- ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
- {
- short itemtype;
- Rect itemrect;
- Handle thandle;
-
- GetDItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
- return((ControlHandle)thandle);
- }
-
- /* This routine checks the range of the number in the given item. If
- it is out of range, the Range alert is invoked, the text is selected,
- and false is returned. */
- Boolean InRange(DialogPtr dptr, short theItem, short min, short max)
- {
- short type, theNum;
- Rect rect;
- Handle hndl;
- Str255 minStr, maxStr;
- Boolean rslt = true;
-
- GetDItem(dptr, theItem, &type, &hndl, &rect);
- if(type == editText)
- {
- theNum = DlogToShort(dptr, theItem);
- if(theNum < min || theNum > max)
- {
- NumToString((long)min, minStr);
- NumToString((long)max, maxStr);
- ParamText(minStr, maxStr, "\p", "\p");
- StopAlert(kRangeAlert, nil);
- SelIText(dptr, theItem, 0, 32767);
- rslt = false;
- }
- }
- return rslt;
- }
-
-
-